package a2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

public class Controller implements ActionListener {
	
	private RotatableJFrame frame;
	private Random rand;

	public Controller(RotatableJFrame frame) {
		this.frame = frame;
		rand = new Random();
	}
	
	/** 
	 * Generates Lorem Ipsum text with `size` many words
	 * @param size - Number of words in the text
	 * @return `size` many words of Lorem Ipsum text
	 */
	private String getLoremText(int size) {
		StringBuilder builder = new StringBuilder();
		
		// append size many words to the String
		for (int i = 0; i < size; i++) {
			// mod is used to wrap around if size > LOREM_IPSUM.length
		    builder.append(Constants.LOREM_IPSUM[i % Constants.LOREM_IPSUM.length] + " ");	
		}
		
		return builder.toString();
	}
	
	
	/**
	 * Performs actions based on ActionEvents
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		
		if (source instanceof JComboBox) {
			JComboBox<?> jcb = (JComboBox<?>) source;
			String text = (String) jcb.getSelectedItem();
			
			this.buildWidget(text);		// build a new widget based on the users selection
			
		}
		
		if (source instanceof JMenuItem) {
			JMenuItem item = (JMenuItem) source;
			String text = item.getText();
			
			switch(e.getActionCommand()) {
			// build a new widget based on the users selection
				case Constants.TEXT_ADD: this.buildWidget(text); break;		
				case Constants.TEXT_EDIT: this.editWidget(text); break;
				case Constants.TEXT_REMOVE: this.removeWidget(text); break;
			} 
			
			if (text.equals(Constants.TEXT_REMOVE_ALL)) {
				frame.removeAllWidgets();
			}
		}
		
		if (source instanceof JRadioButton) {
			JRadioButton b = (JRadioButton) source;
			
			if (b.getText().equals(Constants.ORIENTATION_PORTRAIT) ) {
				frame.setOrientation(true);
			} else if (b.getText().equals(Constants.ORIENTATION_LANDSCAPE)) {
				frame.setOrientation(false);
			} else if (b.getText().equals(Constants.ASPECT_RATIO_16_9)) {
				if (frame.getOrientation()) {
					frame.setAspectRatio(1f/Constants.AR16_9); 
				} else {
					frame.setAspectRatio(Constants.AR16_9); 
				}
			} else if (b.getText().equals(Constants.ASPECT_RATIO_4_3)) {
				if (frame.getOrientation()) {
					frame.setAspectRatio(1f/Constants.AR4_3);
				} else {
					frame.setAspectRatio(Constants.AR4_3);
				}
			} else if (b.getText().equals(Constants.ASPECT_RATIO_3_2)) {
				if (frame.getOrientation()) {
					frame.setAspectRatio(1f/Constants.AR3_2);
				} else {
					frame.setAspectRatio(Constants.AR3_2);
				}
			}
		}
	}
	
	private void editWidget(String text) {
		// TODO Auto-generated method stub

		System.out.println("Editting " + text);
		frame.editWidget((Integer.parseInt(text.substring(0, 1)) - 1));
	}

	private void removeWidget(String text) {
		System.out.println("Removing " + text);
		frame.removeWidget((Integer.parseInt(text.substring(0, 1)) - 1));
		
	}

	private void buildWidget(String text) {
		// Switch on a String is new to Java 7, let's use it to switch on what 
		// the user wants to add to the JFrame. Looks much neater than nested ifs
		switch(text) {
			case Constants.TEXT_JBUTTON: buildJButton(); break;
			case Constants.TEXT_JLABEL: buildJLabel(); break;
			case Constants.TEXT_JTEXTFIELD: buildJTextField(); break;
			case Constants.TEXT_JTEXTAREA: buildJTextArea(); break;
			case Constants.TEXT_JPROGRESSBAR: buildJProgressBar(); break;
			case Constants.TEXT_JSPINNER: buildJSpinner(); break;
			case Constants.TEXT_JSLIDER: buildJSlider(); break;
			case Constants.TEXT_JLIST: buildJList(); break;
			case Constants.TEXT_JTREE: buildJTree(); break;
		}
	}
	
	/** Builds a randomly populated JList with up to 500 entries */
	private void buildJTree() {
		JTree tree = new JTree(new DefaultMutableTreeNode(getLoremText(rand.nextInt(10) + 1)));
		
		for (int i = 0; i < rand.nextInt(250) + 1; i++) {
			DefaultMutableTreeNode node = 
					new DefaultMutableTreeNode(getLoremText(rand.nextInt(10) + 1));
			
			for (int j = 0; j < rand.nextInt(2) + 1; j++) {
				if (rand.nextBoolean()) {
					node.add(new DefaultMutableTreeNode(getLoremText(rand.nextInt(10) + 1)));
				}
				
			}
			
			((DefaultMutableTreeNode) tree.getModel().getRoot()).add(node);
		}
		
		frame.addComponent(tree);
	}

	/** Builds a randomly populated JList with up to 100 entries */
	private void buildJList() {
		frame.addComponent(new JList<String>(getLoremText(rand.nextInt(500) + 1).split(" ")));
	}

	/** Builds a JSlider */
	private void buildJSlider() {
		frame.addComponent(new JSlider());
	}

	/** Builds a JSpinner */
	private void buildJSpinner() {
		frame.addComponent(new JSpinner());
	}

	/** Builds an indeterminate JProgressBar */
	private void buildJProgressBar() {
		JProgressBar jpb = new JProgressBar();
		jpb.setIndeterminate(true);
		frame.addComponent(jpb);
	}

	/** Builds a JTextField with up to 100 words in its text */
	private void buildJTextArea() {
		frame.addComponent(new JTextArea(getLoremText(rand.nextInt(100) + 1)));
	}

	/** Builds a JTextField with up to 10 words in its text */
	private void buildJTextField() {
		frame.addComponent(new JTextField(getLoremText(rand.nextInt(10) + 1)));			
	}

	/** Builds a JLabel with up to 10 words in its text */
	private void buildJLabel() {
		frame.addComponent(new JLabel(getLoremText(rand.nextInt(10) + 1)));			
	}
	
	/** Builds a JButton with up to 10 words in its text */
	private void buildJButton() {
		// Add one to Lorem text to exclude 0 and include 10
		frame.addComponent(new JButton(getLoremText(rand.nextInt(10) + 1)));	
	}

}
